﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.Practices.Unity;
using iHouse.Model.Repository.Interface;
using iHouse.Infrastructure;
using iHouse.Web.Controllers;
using iHouse.Web.Infrastructure;
using iHouse.Model.Data;
using iHouse.Common.Const;
using iHouse.Resource;
using iHouse.Common.Helper;
using iHouse.Common.Configuration;

namespace iHouse.Web.Areas.Admin.Controllers
{
    [Authorize]
    public class AlbumController : BaseController
    {
        protected override CategoryType ControllerType
        {
            get { return CategoryType.Album; }
        }

        [Dependency]
        public IAlbumRepository AlbumRespository { get; set; }

        //
        // GET: Get items for jqGrid
        public ActionResult GetGridData([PaingCriteriaBinder]PagingCriteria pagingCriteria)
        {
            var result = AlbumRespository.GetAlbumsForList(pagingCriteria);

            var formResult = new JqGridList<AlbumFormView>()
            {
                PageIndex = result.PageIndex,
                PageSize = result.PageSize,
                TotalCount = result.TotalCount,
                TotalPages = result.TotalPages
            };

            List<AlbumFormView> albumViews = new List<AlbumFormView>();
            foreach (Album entity in result.Rows)
            {
                albumViews.Add(entity.ToAlbumFormView(this.Categories));
            }

            formResult.Rows = albumViews.ToArray();

            dynamic userdata = new Dictionary<string, string>();
            userdata["CategoryOptions"] = this.Categories.ToJqGridOptions();
            userdata["LanguageOptions"] = LanguageType.Chinese.ToJqGridOptions();
            formResult.UserData = userdata;

            return Json(formResult, JsonRequestBehavior.AllowGet);
        }

        //
        // POST: /Album/Create
        [AcceptVerbs(HttpVerbs.Post)]
        [JsonException]
        public void Create([AlbumBinder]Album album)
        {
            if (ModelState.IsValid)
            {
                AlbumRespository.CreateAlbum(album);
                return;
            }

            throw new Exception();
        }

        //
        // POST: /Album/Update/5
        [AcceptVerbs(HttpVerbs.Post)]
        [JsonException]
        public void Update(int id)
        {
            Album oldAlbum = AlbumRespository.GetAlbumByID(id);
            UpdateModel(oldAlbum);

            if (ModelState.IsValid)
            {
                AlbumRespository.UpdateAlbum();
                return;
            }

            throw new Exception();
        }

        //
        //POST: /Album/Delete/5
        [AcceptVerbs(HttpVerbs.Post)]
        public void Delete(int id)
        {
            AlbumRespository.DeleteAlbum(id);
        }

        [Authorize]
        public ActionResult Upload(int? albumID)
        {
            Album album = this.AlbumRespository.GetAlbumByID(albumID.GetValueOrDefault());

            return View(album);
        }

        [Authorize]
        [HttpPost]
        public ActionResult UploadImage(HttpPostedFileBase image, int? albumID)
        {
            try
            {
                string defaultFolder = FileHelper.CheckPathEndWith(IHouseSection.Current.Upload.AlbumFolder);

                string physicalPath = FileHelper.CheckPathEndWith(Server.MapPath(defaultFolder));
                string[] list = image.FileName.Split('\\');

                image.SaveAs(string.Format("{0}{1}", physicalPath, list.Last()));

                string imagePath = string.Format("{0}{1}", defaultFolder, list.Last());

                Album album = this.AlbumRespository.GetAlbumByID(albumID.GetValueOrDefault());
                album.ImagePath = imagePath;

                this.AlbumRespository.UpdateAlbum();

                return Content(ActionConst.PARENT_PAGE_RELOAD);
            }
            catch (Exception ex)
            {
                return View(ViewConst.ERROR, new HandleErrorInfo(ex, "Album", "UploadCover"));
            }
        }
    }
}
